#include "pseudothread.h"

PseudoThread::PseudoThread()
{
    timer=new QTimer(this);
    loopCount=1;
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
}

PseudoThread::~PseudoThread(){
    stop();
    delete timer;
}

void PseudoThread::start()
{
    timer->start(0);
}

void PseudoThread::stop()
{
    if (timer->isActive()){
        timer->stop();
    }
}

void PseudoThread::setLoopCount(int count)
{
    loopCount=count;
}

void PseudoThread::update()
{
    int loops=loopCount;
    bool workDone=false;
    while (!(workDone=loopStep()) && loops>0){
        loops--;        
    }
    if (workDone){
        stop();     // work is done, stop pseudo thread
    }
}
